import os
from pyeasyaccept.commom.FacadeFactory import FacadeFactory
from pyeasyaccept.gui.ScriptToSave import ScriptToSave

class TestingProject:
    """
    This class represents a testing project which is defined by a business logic Facade and a set 
    of scripts tests.
    """
    
    LINE_SEPARATOR = os.linesep
    SEPARATOR = "::"
    SELECTED = "selected"
    NOT_SELECTED = "not_selected"
        
    def __init__(self, facade):
        """
        Initializes the Project.
        """
        self.file = None
        self.facade = facade
        self.testScripts = []

    def addTestScript(self, testScript):
        """
        Adds a test script to this project.
        """
        self.testScripts.append(testScript)
        
    def getFacade(self):
        """
        Returns the business logica Facade of this project.
        """
        return self.facade
    
    def getFile(self):
        """
        Returns the filename at which this project is persisted.
        """
        return self.file
    
    def setFacade(self, facade):
        """
        Sets the business logic Facade.
        """
        self.facade = facade
    
    def getTestScripts(self):
        """
        Returns the set of test scripts of this project.
        """
        return self.testScripts
        
    def save(self, fileToSave, scripts):
        """
        Save this testing project in a file.
        @param fileToSave: filename
        @param scripts: test scripts to be saved.
        """
        file = open(fileToSave, 'w')
        try:
            file.write("facade:" + self.LINE_SEPARATOR)
            file.write(self.facade + self.LINE_SEPARATOR)
            file.write("testScripts:" + self.LINE_SEPARATOR)
            for test in scripts:
                file.write(test.getFile() + self.SEPARATOR)
                if test.isSelected(): 
                    file.write(self.SELECTED)
                else:
                    file.write(self.NOT_SELECTED)
                file.write(self.LINE_SEPARATOR)
            self.file = fileToSave
        finally:
            file.close()
            
    def open(self, fileToOpen):
        """
        Opens a testing project.
        @param fileToOpen: filename of the project to be opened. 
        """
        try:
            file = open(fileToOpen, 'r')
            line = file.readline().rstrip(self.LINE_SEPARATOR)
            if line == 'facade:':
                self.facade = file.readline().rstrip(self.LINE_SEPARATOR)
            self.testScripts = []
            line = file.readline().rstrip(self.LINE_SEPARATOR)
            if line == 'testScripts:':
                line = file.readline().rstrip(self.LINE_SEPARATOR)
                while line != '':
                    list = line.split(self.SEPARATOR)
                    script = list[0]
                    selected = list[1] == self.SELECTED
                    self.testScripts.append(ScriptToSave(script, selected))
                    line = file.readline().rstrip(self.LINE_SEPARATOR)
            self.file = fileToOpen
        finally:
            file.close()        